﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Xml;
using System.Xml.Schema;
using System.Xml.Linq;

using System.IO;

using sdn.Stuff;

namespace sdn.PRP
{
    public class PRP
    {
        static XDocument xDoc;

        static string ExtraSumTarget = "";
        static string ExtraSumType = "";

        static PRP()
        {
            xDoc = XDocument.Load("data.xml");

            ExtraSumTarget = "Lena";
        }

        public static void XmlValidation()
        {
            #region Schema Inference
            //XmlSchemaInference infer = new XmlSchemaInference();
            //XmlSchemaSet schemaSet = infer.InferSchema(XmlReader.Create("data.xml"));
            //XmlSchema schema = schemaSet.Schemas().Cast<XmlSchema>().First();
            //schema.Write(XmlWriter.Create("data_infered.xsd")); 
            #endregion Schema Inference


            XmlSchemaSet schemaSet = new XmlSchemaSet();
            schemaSet.Add(null, "data.xsd");

            try
            {
                xDoc.Validate(schemaSet, null);
                Console.WriteLine("Document validated successfully.");
            }
            catch (XmlSchemaValidationException ex)
            {
                Console.WriteLine("Exception occurred: {0}", ex.Message);
                Console.WriteLine("Document validated unsuccessfully.");
            }

        }

        public static string XmlPrint()
        {
            string result = "";

            foreach (XElement d in xDoc.Element("Accounting").Elements("Day"))
            {
                result += string.Format("={0}\n\n", (double)d.Attribute("End"));

                if (ExtraSumTarget != "")
                    ExtraSumTarget = GetExtraSum(d, ExtraSumTarget);
                result += string.Format("{0} -{1}{2}\n", ((DateTime)d.Attribute("Date")).ToShortDateString(), (int)d.Attribute("OutSum"), ExtraSumTarget);

                foreach (XElement e in d.Elements())
                {
                    result += FormatInOut(e);
                }

                result += string.Format("\n={0}\n", (double)d.Attribute("Start"));
            }

            return result;
        }
        private static string GetExtraSum(XElement d, string target)
        {
            string result = "";

            double sum = (from l in d.Descendants("Out")
                          where (string)l.Attribute("Target") == target
                          select (double)l.Attribute("Sum")).Sum();
            result = "/-{0}".f(sum);

            return result;
        }
        private static string FormatInOut(XElement e)
        {
            string result = "";

            if (e.Name == "Outs")
            {
                result += string.Format("-{0} {1}\n", (double)e.Attribute("Sum"), (string)e.Attribute("Place"));

                foreach (XElement e2 in e.Elements())
                {
                    result += "\t" + FormatInOut(e2);
                }
            }
            else
            {
                result = string.Format("{0} {1}\n", (double)e.Attribute("Sum"), e.Value.Trim());

                if (e.Name == "In")
                    result = "+" + result;

                if (e.Name == "Out")
                    result = "-" + result;
            }

            return result;
        }
        
        public static void ParseTxt()
        {
            string txt = File.ReadAllText("data.txt");


        }

    }
}
